<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="归属部门" prop="deptId">
        <treeselect
          v-model="queryParams.deptId"
          :options="deptOptions"
          :disable-branch-nodes="false"
          :show-count="true"
          placeholder="请选择归属部门"
          :defaultExpandLevel="2"
          class="search-treeselect"
          style="width: 200px; margin-top: 2px"
        />
      </el-form-item> -->
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select
          v-model="queryParams.deviceType"
          placeholder="请选择设备类型"
          clearable
          size="small"
          @change="getList"
        >
          <el-option
            v-for="dict in dict.type.base_device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="终端通讯号" prop="devicePhoneNum" label-width="90px">
        <el-input
          v-model="queryParams.devicePhoneNum"
          placeholder="请输入808协议终端通讯号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="sim号" prop="simNum">
        <el-input
          v-model="queryParams.simNum"
          placeholder="请输入sim号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="imei" prop="imei">
        <el-input
          v-model="queryParams.imei"
          placeholder="请输入imei"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="在线状态" prop="onlineStatus">
        <el-select
          v-model="queryParams.onlineStatus"
          placeholder="请选择在线状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.dev_online_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-setting" size="mini" @click="handleSetting" :disabled="multiple"
          v-show="queryParams.deviceType == 'work_card'" v-hasPermi="['base:device:set']">设置</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:device:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:device:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:device:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:device:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deviceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column type="index" label="序号" width="60" align="left" />
      <el-table-column
        label="所属部门单位"
        align="left"
        prop="deptName"
        minWidth="200px"
      />
      <el-table-column
        label="设备名称"
        align="left"
        prop="deviceName"
        minWidth="150px"
      />
      <el-table-column
        label="设备类型"
        align="left"
        prop="deviceType"
        minWidth="150px"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.base_device_type"
            :value="scope.row.deviceType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="808协议终端通讯号"
        align="left"
        prop="devicePhoneNum"
        minWidth="150px"
      />
      <el-table-column
        label="sim号"
        align="left"
        prop="simNum"
        minWidth="150px"
      />
      <el-table-column label="imei" align="left" prop="imei" width="150px" />
      <!-- <el-table-column
        label="厂商"
        align="left"
        prop="manufacturer"
        minWidth="150px"
      />
      <el-table-column
        label="型号"
        align="left"
        prop="deviceModel"
        minWidth="150px"
      />
      <el-table-column
        label="协议"
        align="left"
        prop="protocol"
        minWidth="150px"
      /> -->
      <el-table-column
        label="在线状态"
        align="left"
        prop="onlineStatus"
        minWidth="150px"
      >
        <template slot-scope="scope">
          <div style="display: flex; align-items: center">
            <dict-tag
              :options="dict.type.dev_online_status"
              :value="scope.row.onlineStatus"
            />
            <!-- <el-tag v-if="scope.row.deviceType == 'work_card' && scope.row.settingParams == '1'" type="info"
              style="margin-left: 10px;" size="mini">已设置</el-tag> -->
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="数据时间"
        align="left"
        prop="dataTime"
        minWidth="150px"
      />
      <el-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
        width="160px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:device:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:device:remove']"
            >删除</el-button
          >
          <!-- <el-button size="mini" type="text" icon="el-icon-setting" @click="handleSetting(scope.row)"
            v-hasPermi="['base:device:set']" v-if="scope.row.deviceType == 'work_card'">设置</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="deptId">
              <treeselect
                v-model="form.deptId"
                :options="deptOptions"
                :disable-branch-nodes="false"
                :show-count="true"
                placeholder="请选择归属部门"
                :defaultExpandLevel="2"
              />
            </el-form-item>
            <el-form-item label="设备类型" prop="deviceType">
              <el-select
                v-model="form.deviceType"
                placeholder="请选择设备类型"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.base_device_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="手机号" prop="devicePhoneNum">
              <el-input
                v-model="form.devicePhoneNum"
                placeholder="请输入808协议终端通讯号"
              />
            </el-form-item>
            <el-form-item label="sim号" prop="simNum">
              <el-input v-model="form.simNum" placeholder="请输入sim号" />
            </el-form-item>
            <el-form-item label="imei" prop="imei">
              <el-input v-model="form.imei" placeholder="请输入imei" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input
                v-model="form.deviceName"
                placeholder="请输入设备名称"
              />
            </el-form-item>
            <el-form-item label="厂商" prop="manufacturer">
              <el-input v-model="form.manufacturer" placeholder="请输入厂商" />
            </el-form-item>
            <el-form-item label="型号" prop="deviceModel">
              <el-input v-model="form.deviceModel" placeholder="请输入型号" />
            </el-form-item>
            <el-form-item label="协议" prop="protocol">
              <el-input v-model="form.protocol" placeholder="请输入协议" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
            :rows="2"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 设置 -->
    <settingDialog
      :visible.sync="settingDialogVisible"
      :imeis="imeis"
      @refreshList="getList"
    />
  </div>
</template>

<script>
import {
  listDevice,
  getDevice,
  delDevice,
  addDevice,
  updateDevice,
} from "@/api/base/device";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { treeselect } from "@/api/system/dept";

import settingDialog from "./components/settingDialog";

export default {
  name: "Device",
  components: {
    Treeselect,
    settingDialog,
  },
  dicts: ["base_device_type", "dev_online_status"],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        deptId: undefined,
        deviceName: undefined,
        deviceType: undefined,
        devicePhoneNum: undefined,
        simNum: undefined,
        imei: undefined,
        onlineStatus: undefined,
      },
      //部门树
      deptOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceId: [
          { required: true, message: "设备id不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" },
        ],
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        deviceType: [
          { required: true, message: "设备类型不能为空", trigger: "change" },
        ],
        devicePhoneNum: [
          {
            required: true,
            message: "808协议终端通讯号不能为空",
            trigger: "blur",
          },
        ],
        simNum: [{ required: true, message: "sim号不能为空", trigger: "blur" }],
        imei: [{ required: true, message: "imei不能为空", trigger: "blur" }],
        manufacturer: [
          { required: true, message: "厂商不能为空", trigger: "blur" },
        ],
        deviceModel: [
          { required: true, message: "型号不能为空", trigger: "blur" },
        ],
        protocol: [
          { required: true, message: "协议不能为空", trigger: "blur" },
        ],
      },
      settingDialogVisible: false,
      imeis: [],
      imeiList: [],
    };
  },
  computed: {
    deptId() {
      return this.$store.getters.choosedDeptId;
    },
  },
  watch: {
    deptId: function () {
      this.queryParams.deptId = this.deptId;
      this.getList();
    },
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.deptId = this.deptId;
      listDevice(this.queryParams).then((response) => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: undefined,
        deptId: this.deptId,
        deviceName: undefined,
        deviceType: undefined,
        devicePhoneNum: undefined,
        simNum: undefined,
        imei: undefined,
        manufacturer: undefined,
        deviceModel: undefined,
        protocol: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deviceId);
      this.imeiList = selection.map((item) => item.imei);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const deviceId = row.deviceId || this.ids;
      getDevice(deviceId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.deviceId != null) {
            updateDevice(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addDevice(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$modal
        .confirm('是否确认删除"' + (row.orderName || "已勾选") + '"的数据项?')
        .then(() => {
          this.loading = true;
          return delDevice(deviceIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /*手环设备设置*/
    handleSetting(row) {
      if (row.imei) {
        this.imeis = [row.imei];
      } else {
        this.imeis = this.imeiList;
      }
      this.settingDialogVisible = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "base/device/export",
        {
          ...this.queryParams,
        },
        `设备_${new Date().getTime()}.xlsx`
      );
    },
  },
  mounted() {
    if (this.$route.params.imei) {
      this.queryParams.imei = this.$route.params.imei;
      this.getList();
    }
  },
  activated() {
    if (this.$route.params.imei) {
      this.queryParams.imei = this.$route.params.imei;
      this.getList();
    }
  },
};
</script>
